/**
 * 类作用：控制检查视图功能只允许BOM工程师操作
 */

package ext.tianma.part.validators;

import java.util.Locale;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.fc.Persistable;
import wt.fc.WTReference;
import wt.org.WTPrincipal;
import wt.part.WTPart;
import wt.session.SessionHelper;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

public class CheckBOMViewValidators extends DefaultUIComponentValidator{
	
	@Override
	public UIValidationResultSet performFullPreValidation( UIValidationKey uivalidationKey , UIValidationCriteria uivalidationCriteria, Locale locale ) throws WTException {
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		boolean flag = false;
		
		if (uivalidationCriteria != null && uivalidationCriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationCriteria.getContextObject();
			if(null == wtreference){
				uivresultSet.addResult( new UIValidationResult(uivalidationKey, uivalidationstatus ) );
				return uivresultSet;
			}
			Persistable persistable = wtreference.getObject();

			WTPart part = null;			
			if(persistable instanceof WTPart)
			{			
				part = (WTPart) persistable;
			}
			
			if( part != null ){
		
				WTPrincipal principal =  SessionHelper.manager.getPrincipal();
					
				if( principal != null ){
						
					//判断角色对对象是否有完全控制权限,如果有完全控制权限则可以修改“物料详细描述”
					boolean accessControl = AccessControlHelper.manager.hasAccess( principal , part, AccessPermission.ALL );
						
					if( accessControl ){
						flag = true;
					}
				}
			}		
		}
		if (flag) {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.ENABLED));
		} else {
			uivresultSet.addResult(new UIValidationResult(uivalidationKey, UIValidationStatus.HIDDEN));
		}
		
		return uivresultSet;
	}

}
